package com.bvgol.examples.nio;/**
 * @Classname TestFileChannel
 * @Description TODO
 * @Date 2021/6/17 16:59
 * @Created by GUOCHEN
 */

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @program: my-spring-completely
 * @description:
 * @author: GUOCHEN
 * @create: 2021/06/17 16:59
 */
public class TestFileChannelWriteFile {
    public static void main(String[] args) throws Exception {
        FileOutputStream fileOutputStream = null;
        try {
            String s = "hihiyouyou" ;

            //原生io流
            fileOutputStream = new FileOutputStream("D:\\idea_projects\\my-springboot-completely\\netty\\src\\main\\resources\\file01.txt");

            //分配缓冲区
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

            //写入数据
            byteBuffer.put(s.getBytes());
            //反转 由写入变为读取
            byteBuffer.flip();

            //由channel 做最后的io交互
            FileChannel channel = fileOutputStream.getChannel();
            channel.write(byteBuffer);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream!=null){
                fileOutputStream.close();
            }
            System.out.println("执行完毕");
        }
    }
}
